﻿// Write a program that finds in given array of integers a sequence of given sum S (if present). Example:	 
// {4, 3, 1, 4, 2, 5, 8}, S=11 -> {4, 2, 5}	

using System;

class FindSequenceWithGivenSum
{
    static void Main()
    {
        // get the length of the array
        Console.Write("Enter length of the array : ");
        string length = Console.ReadLine();
        int lengthOfArray;
        while (!int.TryParse(length, out lengthOfArray))
        {
            Console.Write("Invalid input. Enter again length for the array : ");
            length = Console.ReadLine();
        }

        // get the wanted sum
        Console.Write("Enter the wanted sum : ");
        string sumStr = Console.ReadLine();
        int wantedSum;
        while (!int.TryParse(sumStr, out wantedSum))
        {
            Console.Write("Invalid input. Enter again the wanted sum : ");
            sumStr = Console.ReadLine();
        }

        if (lengthOfArray < 0)
        {
            Console.WriteLine("Invalid input. The length should be bigger than 0. Start the program again.");
        }
        else
        {

            int[] array = new int[lengthOfArray];

            for (int index = 0; index < lengthOfArray; index++)
            {
                // get the element of the array
                Console.Write("Enter element of the array : ");
                string element = Console.ReadLine();
                int elementOfArray;
                while (!int.TryParse(element, out elementOfArray))
                {
                    Console.Write("Invalid input. Enter element again : ");
                    element = Console.ReadLine();
                }
                array[index] = elementOfArray;
            }

            int temporarySum = 0;
            int startIndex = 0;
            int endIndex = 0;
            bool wantedSumExist = false;
            for (int i = 0; i < lengthOfArray; i++)
            {
                temporarySum = array[i];
                for (int j = i + 1; j < lengthOfArray; j++)
                {
                    temporarySum += array[j];
                    if (temporarySum == wantedSum)
                    {
                        startIndex = i;
                        endIndex = j;
                        wantedSumExist = true;
                    }

                }
            }

            // print the result
            if (wantedSumExist)
            {
                string resultString = "";
                for (int i = startIndex; i <= endIndex; i++)
                {
                    // for the last element of the sequence don't put comma after the element
                    if (i == endIndex)
                    {
                        resultString = resultString + array[i];
                    }
                    else
                    {
                        resultString = resultString + array[i] + ", ";
                    }
                }
                Console.WriteLine("The sequence with the wanted sum is : {" + resultString + "}");
            }
            else
            {
                Console.WriteLine("There is no sequence which sum to be the wanted one.");
            }
        }
    }
}
